package com.darly.book.presenter;


import com.darly.book.model.flag.BookDistillate;
import com.darly.book.model.flag.BookSort;
import com.darly.book.model.flag.BookType;
import com.darly.book.model.remote.BookApi;
import com.darly.book.presenter.contract.DiscReviewContract;
import com.darly.book.ui.base.RxPresenter;
import com.darly.book.utils.Constant;
import com.darly.chinese.db.book.table.BookReviewBean;
import com.darly.dlcommon.common.dlog.DLog;
import com.darly.dlcommon.retrofit.RxjavaRetrofitRequestUtil;

import java.util.List;

import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class DiscReviewPresenter extends RxPresenter<DiscReviewContract.View> implements DiscReviewContract.Presenter {
    private static final String TAG = "DiscReviewPresenter";
    private boolean isLocalLoad = false;

    @Override
    public void firstLoading(BookSort sort, BookType bookType,
                             int start, int limited, BookDistillate distillate) {
        //获取数据库中的数据
//        Single<List<BookReviewBean>> localObserver = LocalRepository.getInstance()
//                .getBookReviews(sort.getDbName(), bookType.getNetName(),
//                        start, limited, distillate.getDbName());
        Single<List<BookReviewBean>> remoteObserver = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                .getBookReviewList("all", sort.getNetName(), bookType.getNetName(),
                        start + "", limited + "", distillate.getNetName())
                .map(listBean -> listBean.getReviews());

//        Single.concat(localObserver, remoteObserver)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(
//                        (beans) -> {
//                            mView.finishRefresh(beans);
//                        }
//                        ,
//                        (e) -> {
//                            isLocalLoad = true;
//                            mView.complete();
//                            mView.showErrorTip();
//                            DLog.e(e);
//                        }
//                        ,
//                        () -> {
//                            isLocalLoad = false;
//                            mView.complete();
//                        }
//                );
    }

    @Override
    public void refreshBookReview(BookSort sort, BookType bookType,
                                  int start, int limited, BookDistillate distillate) {
        Disposable refreshDispo = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                .getBookReviewList("all", sort.getNetName(), bookType.getNetName(),
                        start + "", limited + "", distillate.getNetName())
                .map(listBean -> listBean.getReviews())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        (beans) -> {
                            isLocalLoad = false;
                            mView.finishRefresh(beans);
                            mView.complete();
                        }
                        ,
                        (e) -> {
                            mView.complete();
                            mView.showErrorTip();
                            DLog.e(e);
                        }
                );
        addDisposable(refreshDispo);
    }

    @Override
    public void loadingBookReview(BookSort sort, BookType bookType,
                                  int start, int limited, BookDistillate distillate) {
        if (isLocalLoad) {
            Single<List<BookReviewBean>> single = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                    .getBookReviewList("all", sort.getDbName(), bookType.getNetName(),
                            start + "", limited + "", distillate.getDbName())
                    .map(listBean -> listBean.getReviews());
            loadBookReview(single);
        } else {
            //单纯的加载数据
            Single<List<BookReviewBean>> single = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                    .getBookReviewList("all", sort.getDbName(), bookType.getNetName(),
                            start + "", limited + "", distillate.getNetName())
                    .map(listBean -> listBean.getReviews());
            loadBookReview(single);
        }
    }

    @Override
    public void saveBookReview(List<BookReviewBean> beans) {
//        LocalRepository.getInstance()
//                .saveBookReviews(beans);
    }

    private void loadBookReview(Single<List<BookReviewBean>> observable) {
        Disposable loadDispo = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        (beans) -> {
                            mView.finishLoading(beans);
                        }
                        ,
                        (e) -> {
                            mView.showError();
                            DLog.e(e);
                        }
                );
        addDisposable(loadDispo);
    }
}
